using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenComposite.Repository.Properties;
using OpenComposite.EII.Repository;
using OpenComposite.EII.Forms;

namespace OpenComposite.EII.RepositoryUIs
{
	public partial class FormCreateNewItem : Forms.FormCreateNewBase
	{
		#region Initialization
		protected FormCreateNewItem()
			: base()
		{
			InitializeComponent();
		}
		public FormCreateNewItem(IServiceProvider serviceProvider, Repository.RepositoryItemBase item,
			bool bAllowContinue)
			: base(serviceProvider, item, bAllowContinue)
		{
			InitializeComponent();

			if ( this.RepositoryItem != null ) {
				this.Text = string.Format(Resources.msgCreateNew, this.RepositoryItem.RepositoryItemTypeName);
				//if ( item is GenericFolder ) {
				//    this.AddNullFolderItem = true;
				//    this.NullFolderItemText = "[Add Domain Folder]";
				//}
			}
		}
		public FormCreateNewItem(IServiceProvider serviceProvider, Repository.RepositoryItemBase item,
			bool bAllowContinue, ItemType folderType)
			: base(serviceProvider, item, bAllowContinue, folderType)
		{
			InitializeComponent();

			if ( this.RepositoryItem != null ) {
				this.Text = string.Format(Resources.msgCreateNew, this.RepositoryItem.RepositoryItemTypeName);
				if ( item is GenericFolder ) {
					this.AddNullFolderItem = true;
					this.NullFolderItemText = "[Add Domain Folder]";
				}
			}
		}
		#endregion

		#region Protected Members

		#region Methods

		// override
		protected override void OnValidateNewItem(CancelEventArgs e)
		{
			DataTable dt = Global.Data.SearchRepositoryItems(
				Global.Status.CurrentDomainId, this.RepositoryItem.RepositoryItemType,
				txtName.Text, null, true);
			if ( dt != null && dt.Rows.Count > 0 ) {
				MessageBox.Show("An item with this name exists in the current domain." + Environment.NewLine + Environment.NewLine +
								"Please enter another name.",
								this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
				txtName.Focus();
				txtName.SelectAll();
				e.Cancel = true;
			}
			base.OnValidateNewItem(e);
		}
		protected override void OnSaveClose(CancelEventArgs e)
		{
			save(e);
			base.OnSaveClose(e);
		}
		protected override void OnSaveContinue(CancelEventArgs e)
		{
			save(e);
			base.OnSaveContinue(e);
		}

		#endregion

		#endregion

		#region Private Members

		#region Variables
		#endregion

		#region Methods

		private void save(CancelEventArgs e)
		{
			if ( txtName.Text == "" ) {
				MessageBox.Show("Please enter a name.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
				txtName.Focus();
				// cancel
				if ( e != null )
					e.Cancel = true;
				// return
				return;
			}

			this.RepositoryItem.Name = txtName.Text;
			this.RepositoryItem.Description = txtDesc.Text;

			Repository.RepositoryItemBaseEx itemex = this.RepositoryItem as Repository.RepositoryItemBaseEx;
			if ( itemex != null ) {
				itemex.CreatedType = OpenComposite.EII.Repository.CreatedTypeFlags.UserCreated;
				itemex.DomainId = this.DomainID;
			}
		}

		#endregion

		#region Event Handlers
		private void btnOK_Click(object sender, EventArgs e)
		{
			CancelEventArgs cargs = new CancelEventArgs(false);
			OnValidateNewItem(cargs);
			if ( !cargs.Cancel ) {
				OnSaveClose(new CancelEventArgs(false));
			}
		}
		private void txtName_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
		{
			if ( e.KeyCode == Keys.Enter ) {
				CancelEventArgs cargs = new CancelEventArgs(false);
				OnValidateNewItem(cargs);
				if ( !cargs.Cancel ) {
					OnSaveClose(new CancelEventArgs(false));
				}
			}
		}
		private void txtName_KeyPress(object sender, KeyPressEventArgs e)
		{
			if ( this.RepositoryItem is Documentation ||
				 this.RepositoryItem is GenericFolder ||
				 this.RepositoryItem is Domain ||
				 this.RepositoryItem is Repository.RepositoryObject ) return;
			e.Handled = !Helpers.IsValidCharForName(e.KeyChar);
		}

		private void txtDesc_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
		{
			if ( e.Control && e.KeyCode == Keys.Enter ) {
				CancelEventArgs cargs = new CancelEventArgs(false);
				OnValidateNewItem(cargs);
				if ( !cargs.Cancel ) {
					OnSaveClose(new CancelEventArgs(false));
				}
			}
		}

		#endregion

		#endregion
	}
}